
package ch.hearc.coursJava.exa2;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import com.bilat.tools.reseau.rmi.RmiTools;

public class Chat extends JFrame implements Chat_I
	{

	/**
     * 
     */
    private static final long serialVersionUID = 3022827834168297251L;
    /*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public Chat(RMISettings client, RMISettings server)
		{
		server(server);
		client(client);


		creerComposants();
		placerComposant();
		addListener();

		this.setLocation(0, 0);
		this.setTitle("Table � dessin");
		this.setMinimumSize(new Dimension(800, 600));
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setVisible(true); //Par d�faut: NON-visible, A mettre en dernier
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/
	@Override
	public void put(MyString texte) throws RemoteException
		{
		this.txtArea.append("\r\n"+texte);
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	private void client(RMISettings clientSettings)
		{
		this.client = (Chat_I)RmiTools.connectionRemoteObjectBloquant
					(
						clientSettings.getAddress().getHostName(),
						clientSettings.getId(),
						clientSettings.getPort()
					);
		}

	private void server(RMISettings serverSettings)
		{
		try
			{
			RmiTools.shareObject(this, serverSettings.getPort(), serverSettings.getId());
			}
		catch (RemoteException e)
			{
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
		}

	private void placerComposant()
		{
		BorderLayout borderLayout = new BorderLayout();
		setLayout(borderLayout);

		add(txtArea, BorderLayout.CENTER);
		add(txtField, BorderLayout.SOUTH);
		add(btnSend, BorderLayout.EAST);
		}

	private void creerComposants()
		{
		btnSend = new JButton("Envoyer");
		txtArea = new JTextArea();
		txtArea.setEnabled(false);
		txtField = new JTextField();
		}

	private void addListener()
		{
		btnSend.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0)
					{
					try
						{
						client.put(new MyString(txtField.getText()));
						txtArea.append("\r\n"+txtField.getText());
						txtField.setText("");
						}
					catch (Exception e)
						{
						// TODO: handle exception
						}
					}
			});

		this.addKeyListener(new KeyAdapter()
			{

				@Override
				public void keyReleased(KeyEvent e)
					{
					if(e.getKeyCode() == KeyEvent.VK_ENTER)
						{
						btnSend.doClick();
						}
					}
			});

		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/
	private JButton btnSend;
	private JTextArea txtArea;
	private JTextField txtField;
	private Chat_I client;

	}

